package cn.wolfcode.web.controller;

import cn.wolfcode.qo.ConsumptionReportQueryObject;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.IConsumptionReportService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/consumptionReport")
public class ConsumptionReportController {

    @Autowired
    private IConsumptionReportService consumptionReportService;

    @Autowired
    private IBusinessService businessService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") ConsumptionReportQueryObject qo) {

        model.addAttribute("businessService", businessService.listAll());

        PageInfo pageInfo = consumptionReportService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "consumptionReport/list";
    }

    @RequestMapping("/listBar")
    public String listBar(Model model, @ModelAttribute("qo") ConsumptionReportQueryObject qo) {

//        List<Map<String, Object>> mapList = consumptionReportService.queryForChar(qo.getGroupType());
        qo.setPageSize(Integer.MAX_VALUE);
        PageInfo<Map<String, Object>> pageInfo = consumptionReportService.query(qo);



        List<Object> groupTypeMap = new ArrayList<>();

        List<Object> totalAmountList = new ArrayList<>();

        List<Object> discountAmout = new ArrayList<>();

        List<Object> payAmount = new ArrayList<>();

        List<Object> count = new ArrayList<>();
        for (Map<String, Object> map : pageInfo.getList()) {
            groupTypeMap.add(map.get("groupType"));
            totalAmountList.add(map.get("totalAmount"));
            payAmount.add(map.get("payAmount"));
            discountAmout.add(map.get("discountAmout"));
            count.add(map.get("count"));
        }
        model.addAttribute("groupTypeMap", JSON.toJSONString(groupTypeMap));
        model.addAttribute("totalAmountList", JSON.toJSONString(totalAmountList));
        model.addAttribute("payAmount", JSON.toJSONString(payAmount));
        model.addAttribute("discountAmout", JSON.toJSONString(discountAmout));
        model.addAttribute("count", JSON.toJSONString(count));

        return "consumptionReport/listBar";
    }
}
